(function() {

	var util = load('js/server/util.js');

	var NamePairs = function(pairs) {
		util.extend(this, pairs);
	}
	util.extend(NamePairs.prototype, {
		getFirst : function(name) {
			if(this[name]) {
				return this[name][0];
			} 
			return null;
		}
	});

	Request = function(r) {
		util.extend(this, r);
		this.params = new NamePairs(this.params);
		this.headers = new NamePairs(this.headers);
	}

	Response = function(r) {
		util.extend(this, r);
		this.headers = new NamePairs(this.headers);
	}

	var wrapChain = function(chain) {
		return function(req) {
			var json = JSON.stringify(req);
			var jReq = com.googlecode.restitory.gae.RequestJsoner
					.parseRequestJson(json);
			var ret = chain.copy().execute(jReq);
			ret = com.googlecode.restitory.gae.RequestJsoner.formatJson(ret);
			ret = JSON.parse(ret);
			return ret;
		}
	}

	return function(r, chain) {
		var f = repo.source('/hook.js');
		var c = wrapChain(chain);
		var json = com.googlecode.restitory.gae.RequestJsoner.formatJson(r);
		var req = JSON.parse(json);
		req = new Request(req);

		var ret = f(req, c);
		json = JSON.stringify(ret);
		var resp = com.googlecode.restitory.gae.RequestJsoner
				.parseResponseJson(json);
		return resp;
	}

})();